package com.rico.deploy.controller;

import com.github.pagehelper.PageInfo;
import com.rico.common.response.MessageUtils;
import com.rico.common.response.RestResponse;
import com.rico.deploy.entity.Term;
import com.rico.deploy.service.TermService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @author 夏智力
 * @FileName TermController
 * @date 2021/5/6 15:05
 * @Software: IntelliJ IDEA
 */
@Controller
@Slf4j
public class TermController {

    @Autowired
    private TermService termService;

    @GetMapping("term/list")
    @ResponseBody
    public RestResponse getlist(Integer page, Integer limit) {
        List<Term> terms = termService.selectAll(page, limit);
        PageInfo<Term> termPageInfo = new PageInfo(terms);
        return MessageUtils.success(termPageInfo.getList(), (int) termPageInfo.getTotal());
    }


    @GetMapping("term/delete")
    @ResponseBody
    public RestResponse deleteById(Integer id) {
        log.info("{}", id);
        String msg = null;
        if ( termService.deleteTermById(id) == 1 )
            msg = "删除成功";
        else
            msg = "删除失败";
        return MessageUtils.success(msg);
    }


    @PostMapping("term/put")
    @ResponseBody
    public RestResponse put(Term term) {

        int i = termService.updateTerm(term);
        if ( i == 0 )
            return MessageUtils.fail("编辑失败");

        else
            return MessageUtils.success("编辑成功");
    }

    @PostMapping("term/post")
    @ResponseBody
    public RestResponse post(Term term) {

        int i = termService.insertTerm(term);
        if ( i == 0 )
            return MessageUtils.fail("添加失败");

        else
            return MessageUtils.success("添加成功");
    }

    /**
     * 获取html页面
     * @return
     */
    @GetMapping("termManage")
    public String gethtml(){
        return "page/termManage";
    }


}
